<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>短信发送量统计分析</title>
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../js/jquery-1.10.2.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/util.js"></script>
    <script>
        layui.use(['laypage', 'layer', 'table', 'form', 'jquery', 'laydate', 'element'], function () {
            var table = layui.table;
            var laypage = layui.laypage;
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.$;
            var laydate = layui.laydate;
            
            var tableData = {
                elem: '#table',
                id: "modelReload", //id
                url: '/smsReportTotal/find?countWay=mgr',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                }
                , limit: 10,
                limits: [10, 20, 30, 40, 50],
                layout: ['prev', 'page', 'next'],
                prev: "上一页",
                next: "下一页",
                page: true //开启分页
                , cols: [[
                    {field: 'cityName', title: '地市', rowspan: 2}
                    ,{field: 'countyName', title: '区县', rowspan: 2}
                    , {field: 'mgrName', title: '项目名称', rowspan: 2}
                     , {field: 'sender', title: '短信通道号', rowspan: 2}
                    , {title: '长短信', colspan: 3, align: 'center'}
                    , {title: '正常短信', sort: true, colspan: 2, align: 'center'}
                    , {title: '费用统计', sort: true, colspan: 2, align: 'center'}                   
                    , {title: '外地市用户', colspan: 2, align: 'center'}
                    , {title: '外省用户', colspan: 2, align: 'center'}], [
                    {field: 'longNum', title: '发送量'}
                    , {field: 'longNumSplit', title: '长短信拆分后数量'}
                    , {field: 'longPercent', title: '占比'}
                    , {field: 'normalNum', title: '发送量'}
                    , {field: 'normalPercent', title: '占比'}
                    , {field: 'totalCount', title: '总发送量'}
                    , {field: 'totalFee', title: '总费用(总发送量*0.03元/条)'}
                    , {field: 'otherCities', title: '发送量'}
                    , {field: 'otherCitiesPercent', title: '占比'}
                    , {field: 'otherProvinces', title: '发送量'}
                    , {field: 'otherProvincesPercent', title: '占比'}
                ]]
                ,done:function (res) {   //返回数据执行回调函数
   					layer.closeAll('loading'); //关闭loading 
				}
            };

            //根据城市ID查询项目
            function selectMgrInfo(cityId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
			//动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该地市下没有项目</option>');
                }
                $('#regionId').html('<option value="">请先选择项目</option>');
                $('#stationId').html('<option value="">请先选择区域</option>');
                form.render();
            };
			
	   $("#mgrName").autocomplete({
       source : function( request, response ) {
        var key = $("#mgrName").val();
        var cityId=$("#cityId").val();
        var countyId=$("#countyId").val();
        var url = '/util/selectMgrNameLike';
        var param = {
            cityId : cityId,
            countyId : countyId,
         key : key
        };
                 $.ajax({
         type : 'post',
         async : false,
         url : url,
         data : param,
         success : function(json) {
          var data = json.data;
          source = [];
          for (var i = 0; i < data.length; i++) {
           source[i] = data[i].name;
          }
          console.log(source);
          response( source );
         },
         error : function() {
          layer.msg('系统异常，请联系管理员处理!');
         }
        }
        );
            },
       messages : { //message设置不出现匹配关键字的结果
        noResults : '',
        results : function() {
        }
       }
      });

			//监听项目  区县
            form.on('select(cityId)', function (data) {
            	var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data = selectMgrInfo(data.value);
                appendMgr(data);
            });
            //监听项目
            form.on('select(countyId)', function (data) {
            	var data1;
            	if(null!=data.value && ""!=data.value){
            		data1 = selectMgrInfo(data.value);
            	}else{
            		var cityId = $("#cityId").val();
            		data1 = selectMgrInfo(cityId);
            	}
                appendMgr(data1);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            //身份识别
            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                    console.info(json);
                    	if(json.duty !=0){
                tableData = {
                elem: '#table',
                id: "modelReload2", //id
                url: '/smsReportTotal/find?countWay=mgr',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                }
                , limit: 10,
                limits: [10, 20, 30, 40, 50],
                layout: ['prev', 'page', 'next'],
                prev: "上一页",
                next: "下一页",
                page: true //开启分页
                , cols: [[
                    {field: 'cityName', title: '地市', rowspan: 2}
                    ,{field: 'countyName', title: '区县', rowspan: 2}
                    , {field: 'mgrName', title: '项目名称', rowspan: 2}
                     , {field: 'sender', title: '短信通道号', rowspan: 2}
                    , {title: '长短信', colspan: 3, align: 'center'}
                    , {title: '正常短信', sort: true, colspan: 2, align: 'center'}
                    , {field : 'totalCount',title : '总发送量',rowspan : 2}
                    //, {title: '费用统计', sort: true, colspan: 2, align: 'center'}                   
                    , {title: '外地市用户', colspan: 2, align: 'center'}
                    , {title: '外省用户', colspan: 2, align: 'center'}], [
                    {field: 'longNum', title: '发送量'}
                    , {field: 'longNumSplit', title: '长短信拆分后数量'}
                    , {field: 'longPercent', title: '占比'}
                    , {field: 'normalNum', title: '发送量'}
                    , {field: 'normalPercent', title: '占比'}
                    /* , {field: 'totalCount', title: '总发送量'}
                    , {field: 'totalFee', title: '总费用(总发送量*0.03元/条)'} */
                    , {field: 'otherCities', title: '发送量'} 
                    , {field: 'otherCitiesPercent', title: '占比'}
                    , {field: 'otherProvinces', title: '发送量'}
                    , {field: 'otherProvincesPercent', title: '占比'}
                ]]
                ,done:function (res) {   //返回数据执行回调函数
   					layer.closeAll('loading'); //关闭loading 
				}
            };
                    	}
                        if (json.duty == 1) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(data);
                            form.render();
                        } else if (json.duty == 2) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(data);
                            form.render();
                        } else if (json.duty == 3) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(json.list);
                            form.render();
                        }
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });

            
            $('#query').on('click', function () {
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                var way = $("input[name='way']:checked").val();
                var mgrId = $("#mgrId").val();
                var mgrName = $("#mgrName").val();
                if (judetmentDate(startTime, endTime) && way == 1) {
                    layer.msg("您选择的时间超出了30天，请重新选择");
                } 
                else {
                    var index = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    layer.load(); //上传loading
                    
                    $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        if (json.duty ==0) {
                        	table.reload('modelReload', {
                        method: 'post',
                        page: {
                            curr: 1
                        },
                        where: {
                            way: $("input[name='way']:checked").val(),
                            startTime: formatDate(startTime),
                            endTime: formatDate(endTime),
                            month: $("#yearmonth").val(),
                            year: $("#year").val(),
                            cityId: $("#cityId").val(),
                            countyId: $("#countyId").val(),
                            mgrId: $("#mgrId").val(),
                            mgrName: $("#mgrName").val()
                        }
                    });
                        }else{
                        	table.reload('modelReload2', {
                        method: 'post',
                        page: {
                            curr: 1
                        },
                        where: {
                            way: $("input[name='way']:checked").val(),
                            startTime: formatDate(startTime),
                            endTime: formatDate(endTime),
                            month: $("#yearmonth").val(),
                            year: $("#year").val(),
                            cityId: $("#cityId").val(),
                            countyId: $("#countyId").val(),
                            mgrId: $("#mgrId").val(),
                            mgrName: $("#mgrName").val()
                        }
                    });
                        }
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
                    
                }
            });
            //导出
            $('#download').on('click', function () {
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                var way = $("input[name='way']:checked").val();
                if (judetmentDate(startTime, endTime) && way == 1) {
                    layer.msg("您选择的时间超出了30天，请重新选择");
                } else {
                    var url = "/smsReportTotal/download?countWay=mgr"
                        + "&way=" + way
                        + "&startTime=" + formatDate(startTime)
                        + "&endTime=" + formatDate(endTime)
                        + "&month=" + $("#yearmonth").val()
                        + "&cityId=" + $("#cityId").val()
                        + "&mgrId=" + $("#mgrId").val()
                        + "&mgrName=" + $("#mgrName").val()
                        + "&year=" + $("#year").val();
                    location.href = url;
                }
            });
            //时间下拉框设置
            //时间选择器的样式
            //获取系统时间，做时间选择
            //获取指定月的天数
            var myDate = new Date();
            //获取当前的年份
            var year = myDate.getFullYear();
            //获取当前月的月份

            var month = myDate.getMonth() + 1;
            //上一月的年份
            var inityear = year ;
            //上个月的月份
            var initmonth = month ;

            initmonth = (initmonth < 10 ? 0 + "" + initmonth : initmonth);

            //初始化最大月份
            var maxdate = inityear + '-' + initmonth + '-' + 25;

            //前6个月的月份
            var minmonth = month - 6;

            //前6个月的年份
            var minyear = (minmonth > 0 ? year : year - 1);

            minmonth = (minmonth > 0 ? minmonth : minmonth + 12);

            //初始化最小月份
            var mindate = minyear + '-' + minmonth + '-' + 01;
            var maxtime = inityear + '-' + initmonth + "-" + myDate.getDate();
            var mintime = minyear + '-' + minmonth + "-01";
            var initiaSartTime = inityear + "-" + initmonth + "-" + '01';
            var initiaEndTime = inityear + "-" + initmonth + "-" + myDate.getDate();
            var init = inityear + "" + initmonth;
			laydate.render({
                elem: '#year'
                , type: 'year'
                , btns: ['clear', 'confirm']
                , trigger: 'click'
                , format: 'yyyy'
                , value: inityear
                , max: maxtime
                , min: mintime
            });
            laydate.render({
                elem: '#yearmonth'
                , type: 'month'
                , btns: ['clear', 'confirm']
                , trigger: 'click'
                , format: 'yyyyMM'
                , value: init
                , max: maxdate
                , min: mindate
            });
            laydate.render({
                elem: '#startTime'
                , type: 'date'
                , btns: ['clear', 'confirm']
                , trigger: 'click'
                , value: initiaSartTime
                , max: maxtime
                , min: mintime
            });
            laydate.render({
                elem: '#endTime'
                , type: 'date'
                , max: maxtime
                , trigger: 'click'
                , btns: ['clear', 'confirm']
                , value: initiaEndTime
                , max: maxtime
                , min: mintime
            });
            table.render(tableData);
            table.on('tool(demoEvent)', function (obj) {
                var data = obj.data;
                if (obj.event === 'setSign') {
                    layer.open({
                        type: 2,
                        title: '外地人口发送统计',
                        shadeClose: true,
                        shade: false,
                        area: ['800px', '500px']
                        , content: ['fieldCount.html?cityId=' + data.cityId + "&mgrId=" + data.mgrId
                        + "&way=" + $("input[name='way']:checked").val()
                        + "&month=" + $("#yearmonth").val()
                        + "&startTime=" + formatDate($("#startTime").val())
                        + "&endTime=" + formatDate($("#endTime").val()), 'no']
                    });
                }
            });
        });
    </script>
</head>
<body>
<!--nav-->
<div class="layui-html-limit">
    <!--container-->
    <div class="layui-content-body">
        <div class="scroll-box">
            <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
 <a href="javascript:returnIndex2()">首页&nbsp;</a>
  <a href="">&nbsp;报表管理&nbsp;</a>
  <a href="">&nbsp;短信发送量统计分析&nbsp;</a>
  <a href="">&nbsp;按项目统计分析&nbsp;</a>
</span>
            </div>
            <div class="content-box ">
                <div class="form-select-box">
                    <div class="layui-form layui-form-pane">
                        <div class="layui-form-item">
                            <div class="layui-inline" pane="">
                                <label class="layui-form-label">按统计方式</label>
                                <div class="layui-input-block">
                                    <a href="/view/reportManage/countByCity.html" id="city"><input type="radio" name="countWay" title="按地市"></a>
                                    <a href="/view/reportManage/countByCounty.html" id="county"><input type="radio" name="countWay" title="按区县"></a>                                                               
                                    <a href="/view/reportManage/countByType.html" id="type">
										<input type="radio" name="countWay" title="按类型"></a> 
                                    <a href="/view/reportManage/countByMgr.html" id="mgr"><input type="radio" name="countWay" title="按项目" checked=""></a>
                                    <a href="/view/reportManage/countByContent.html" id="content"><input type="radio" name="countWay" title="按内容"></a> 
                                    <a href="/view/reportManage/countByRegion.html" id="region"><input type="radio" name="countWay" title="按区域"></a>
                                    <a href="/view/reportManage/countByStation.html" id="station"><input type="radio" name="countWay" title="按基站"></a> </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">地市</label>
                                <div class="layui-input-block">
                                    <select name="cityId" lay-filter="cityId" id="cityId">
                                        <option value="">请选择</option>
                                        <option value="11">苏州</option>
                                        <option value="12">淮安</option>
                                        <option value="13">宿迁</option>
                                        <option value="14">南京</option>
                                        <option value="15">连云港</option>
                                        <option value="16">徐州</option>
                                        <option value="17">常州</option>
                                        <option value="18">镇江</option>
                                        <option value="19">无锡</option>
                                        <option value="20">南通</option>
                                        <option value="21">泰州</option>
                                        <option value="22">盐城</option>
                                        <option value="23">扬州</option>
                                        <option value="25">省级</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
		                            <label class="layui-form-label">区县</label>
		                            <div class="layui-input-block">
		                                <select name="countyId" lay-filter="countyId" id="countyId">
		                                    <option value="">全部</option>
		                                </select>
		                            </div>
		                	</div>
                            <div class="layui-inline">
                                <label class="layui-form-label">项目名称</label>
                                <div class="layui-input-block">
                                    <select name="interest" lay-filter="mgrId" id="mgrId">
                                        <option value="">请先选择地市</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline" id="shortname">
                                <label class="layui-form-label">项目简称</label>
                                <div class="layui-input-block">
                                    <input type="text"  name="mgrName" id="mgrName" lay-verify="required" value="" placeholder="请输入"
                                           autocomplete="on" class="layui-input" />
                                     
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">发送时间</label>
                            <div class="layui-inline">
                            	<div class="layui-inline">
                                    <div class="layui-inline">
                                        <input type="radio" name="way" value="2" title="按年" checked="">
                                    </div>
                                    <div class="layui-inline">
                                        <input type="text" id="year" placeholder="请选择年份" class="layui-input"
                                               style="width: 100px;" readonly>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-inline">
                                        <input type="radio" name="way" value="0" title="按月" checked="">
                                    </div>
                                    <div class="layui-inline">
                                        <input type="text" id="yearmonth" placeholder="请选择月份" class="layui-input"
                                               style="width: 100px;" readonly>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <div class="layui-inline">
                                            <input type="radio" name="way" value="1" title="按时间段">
                                        </div>
                                        <div class="layui-inline">
                                            <div class="layui-input-inline" style="width: 130px;">
                                                <input type="text" name="date" id="startTime" autocomplete="off"
                                                       class="layui-input" readonly>
                                            </div>
                                            <div class="layui-form-mid">-</div>
                                            <div class="layui-input-inline" style="width: 130px;">
                                                <input type="text" name="date" id="endTime" autocomplete="off"
                                                       class="layui-input" readonly>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn layui-btn-normal" type="button" id="query">
                                            查询
                                        </button>
                                        <button class="layui-btn layui-btn-normal" type="button" id="download">
                                            导出
                                        </button>
                                    </div>
                                    <div style="color: red">短信汇总统计数据只保存6个月，查询时间段不可超出30天</div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
                <!--表格-->
                <div class="table-box">
                    <table class="layui-hide" id="table" lay-filter="demoEvent">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>